home *** CD-ROM | disk | FTP | other *** search
/ C/C++ Users Group Library 1996 July / C-C++ Users Group Library July 1996.iso / listings / v_11_03 / 1103077a < prev    next >
Text File  |  1993-01-03  |  1KB  |  50 lines

  1. // COR.CPP
  2. #include "cor.hpp"
  3. #include ...
  4.  
  5. extern void ctxtswc(void);
  6. coroutine *MAINC=new coroutine();
  7. coroutine *PREVIOUS,*CURRENT=MAINC;
  8.  
  9. coroutine::coroutine() {
  10.     word *sp,*stkBase;
  11.     stkSize= 2 * sizeof(word);
  12.     stkBase=(word *) farmalloc(stkSize);
  13.     stkSegment=FP_SEG(stkBase);
  14.     stkOffset=FP_OFF(stkBase);
  15.     sp=stkBase + 2;
  16.     *--sp=(word) startProcess;}
  17.  
  18. coroutine::~coroutine() {
  19.     delete(MK_FP(stkSegment,stkOffset));}
  20.  
  21. void coroutine::superMain(void) {
  22.     main();
  23.     resume(MAINC);
  24.     FATAL("terminated coroutine resumed");}
  25.  
  26. void startProcess(void) {
  27.     CURRENT->superMain();}
  28.  
  29. void resume(coroutine* rc)
  30. {    word sp,*stkBase;
  31.     if ((CURRENT != NULL) && (rc != CURRENT) &&
  32.         (rc != NULL)) {
  33.         sp=_SP;
  34.         CURRENT->stkSize= _stklen - sp + 4;
  35.         stkBase=
  36.            (word*)farmalloc(CURRENT->stkSize);
  37.         CURRENT->stkSegment=FP_SEG(stkBase);
  38.         CURRENT->stkOffset=FP_OFF(stkBase);
  39.         PREVIOUS=CURRENT;
  40.         CURRENT=rc;
  41.         ctxtswc();
  42.         delete(MK_FP(CURRENT->stkSegment,
  43.                      CURRENT->stkOffset));
  44.     }
  45. }
  46.  
  47. void detach(void) {
  48.     resume(MAINC);}
  49.  
  50.